﻿using System;
using RAP.Framework.Libary.Utils;
using System.Collections;
using RAP.Framework.Libary.DomTemplateBuilder.Element.Collection;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler
{
	/// <summary>
	/// 字符串分割器
	/// </summary>
	public class StringSpliterHandler : IterateHandler
    {
        /// <summary>
        /// 
        /// </summary>
        public new virtual StringSpliter Element
        {
            get
            {
                return base.Element as StringSpliter;
            }
        }

        public override object GetPropertyValue ( object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
            var value = base.GetPropertyValue ( rootParamObject, parentParamObject, context );
            if ( value == null )
            {
                return null;
            }
			var splitString = this.Element.SplitString;
			if ( splitString.IsNullOrEmpty ( ) )
            {
                return null;
            }
			var values = value.ToString ( ).Split ( new string[ ] { splitString }, StringSplitOptions.RemoveEmptyEntries );
			var result = new ArrayList ( );
            foreach ( var str in values )
            {
				result.Add ( str );
            }
            return result;
        }
    }
}
